﻿
#include "IM_Login.h"
#include "IM_Agent.h"
#include <string>
#include "IM_Account.h"

std::string  IM_Login::login_check(char *buf, int size)
{
	IM_Protocol::LoginRequest login_request;
	login_request.ParseFromArray(buf, size);
	auto ac = login_request.ac();
	auto pas = login_request.paw();
	DEBUG("ac:%s\n", ac.c_str());
	DEBUG("paw:%s\n", pas.c_str());

	if (_check_account_password(ac, pas))
	{
		return std::move(ac);
	}
	return std::string();
}

void IM_Login::login_response_succes(IM_Agent* agent, const std::string & token)
{
	IM_Protocol::LoginRespond r;
	r.set_token(token.c_str(), token.length());
	r.set_rcode(LoginRsultCode_sucess);
	char buf[1024];
	uint16_t type = 1;
	memcpy(buf, &type, 2);
	r.SerializeToArray(buf + 2, r.ByteSize());
	agent->send_data_by_socket_tcp(buf, r.ByteSize() + 2);
	DEBUG("发送 token \n");
}

void IM_Login::login_response_faile(IM_Agent* agent,int code)
{
	IM_Protocol::LoginRespond r;
	r.set_rcode(code);
	char buf[64];
	uint16_t type = 1;
	memcpy(buf, &type, 2);
	r.SerializeToArray(buf + 2, r.ByteSize());
	agent->send_data_by_socket_tcp(buf, r.ByteSize() + 2);
	DEBUG("发送 登陆错误 \n");
}

bool IM_Login::_check_account_password(const std::string ac, const std::string pas)
{
	if (ac == "10000"&&pas == "lzm123")
	{
		return true;
	}
	else
	{
		return false;
	}
}

